package cn.lyq.card.utils

import android.content.ComponentName
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore
import java.io.File


class ShareUtils {

    companion object {
        private const val PACKAGE_WE_CHAT = "com.tencent.mm"
        private const val PACKAGE_WE_CHAT_CLS = "com.tencent.mm.ui.tools.ShareImgUI"
        private const val PACKAGE_WE_CHAT_CIRCLE_CLS = "com.tencent.mm.ui.tools.ShareToTimeLineUI"
        private const val PACKAGE_MOBILE_QQ = "com.tencent.mobileqq"
        private const val PACKAGE_MOBILE_QQ_CLS = "com.tencent.mobileqq.activity.JumpActivity"
        private const val PACKAGE_QZONE = "com.qzone"
        private const val PACKAGE_SINA = "com.sina.weibo"

        // 判断是否安装指定app
        fun isInstallApp(context: Context, app_package: String): Boolean {
            val packageManager = context.packageManager
            val pInfo = packageManager.getInstalledPackages(0)
            for (i in pInfo.indices) {
                val pn = pInfo[i].packageName
                if (app_package == pn) {
                    return true
                }
            }
            return false
        }

        fun getImgUri(context: Context, imageFile: File): Uri? {
            val filePath = imageFile.absolutePath
            val cursor: Cursor? = context.contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                arrayOf(MediaStore.Images.Media._ID),
                MediaStore.Images.Media.DATA + "=? ",
                arrayOf(filePath),
                null
            )
            return if (cursor != null && cursor.moveToFirst()) {
                val id: Int = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID))
                val baseUri: Uri = Uri.parse("content://media/external/images/media")
                Uri.withAppendedPath(baseUri, "" + id)
            } else {
                if (imageFile.exists()) {
                    val values = ContentValues()
                    values.put(MediaStore.Images.Media.DATA, filePath)
                    context.contentResolver
                        .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
                } else {
                    null
                }
            }
        }

        fun build() = ShareUtils()
    }

    private val intent: Intent = Intent(Intent.ACTION_SEND)

    fun shareQQ(): ShareUtils {
        intent.component = ComponentName(PACKAGE_MOBILE_QQ, PACKAGE_MOBILE_QQ_CLS)
        return this
    }

    fun shareToSina(context: Context): ShareUtils {
        val packageManager: PackageManager = context.packageManager
        val matchInfo = packageManager.queryIntentActivities(
            intent,
            PackageManager.MATCH_DEFAULT_ONLY
        )
        var resolveInfo: ResolveInfo? = null
        for (each in matchInfo) {
            val pkgName = each.activityInfo.applicationInfo.packageName
            if ("com.sina.weibo" == pkgName) {
                resolveInfo = each
                break
            }
        }
        resolveInfo?.let {
            intent.setClassName(PlatformUtil.PACKAGE_SINA, resolveInfo.activityInfo.name)
        }
        return this
    }

    fun shareWeChatFriend(): ShareUtils {
        intent.component = ComponentName(PACKAGE_WE_CHAT, PACKAGE_WE_CHAT_CLS)
        return this
    }

    fun shareWeChatFriendCircle(): ShareUtils {
        intent.component = ComponentName(PACKAGE_WE_CHAT, PACKAGE_WE_CHAT_CIRCLE_CLS)
        return this
    }

    fun shareImage(uri: Uri): ShareUtils {
        intent.putExtra(Intent.EXTRA_STREAM, uri)
        intent.type = "image/*"
        return this
    }

    fun shareTextImage(uri: Uri, content: String): ShareUtils {
        intent.putExtra(Intent.EXTRA_STREAM, uri)
        intent.putExtra(Intent.EXTRA_TEXT, content)
        intent.type = "image/*"
        return this
    }

    fun shareText(content: String): ShareUtils {
        intent.putExtra(Intent.EXTRA_TEXT, content)
        intent.type = "text/plain"
        return this
    }

    fun create(): Intent {
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        return Intent.createChooser(intent, "Share")
    }
}